//
//  WSJSportViewController.m
//  RunningMan
//
//  Created by dllo on 16/5/17.
//  Copyright © 2016年 WSJ. All rights reserved.
//

#import "WSJSportViewController.h"
#import "WSJSportView.h"
#import "WSMyTool.h"
#import "WSJSportBeginViewController.h"
#import "WSJSportDataRecordViewController.h"
#import "WSMusicViewController.h"
@interface WSJSportViewController ()

@property(nonatomic, retain)WSJSportView *sportView;
@property(nonatomic, retain)RMCoreData *coreDataManager;


@end

@implementation WSJSportViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    
    self.coreDataManager = [RMCoreData defaultCoreDataManager];
    
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"RunData" inManagedObjectContext:self.coreDataManager.managedObjectContext];
    [fetchRequest setEntity:entity];
    
    NSError *error = nil;
    NSArray *fetchedObjects = [self.coreDataManager.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    
    CGFloat distance = 0;
    CGFloat time = 0;

    if (fetchedObjects == nil) {
        
        NSLog(@"暂无数据");
    } else {
        for (RunData *model in fetchedObjects) {
            distance += [model.distance floatValue];
            time += [model.runtime floatValue];
       
        }
        
        self.sportView.labelTime.text = [NSString stringWithFormat:@"%0.2f", time / 3600];
        self.sportView.labelMileage.text = [NSString stringWithFormat:@"%0.2f", distance];
        self.sportView.labelStep.text = [NSString stringWithFormat:@"%.f", distance * 1000 * 100 / 60];
        self.sportView.labelNumber.text = [NSString stringWithFormat:@"%ld", fetchedObjects.count];
  
    }
    
}



- (void)viewDidLoad {
    [super viewDidLoad];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 30, 30);
    [button setBackgroundImage:[UIImage imageNamed:@"plist.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(leftButtonAction) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = leftButton;
    
    UIImageView *imageView =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"flag1.png"]];
    imageView.frame = CGRectMake(0, 0, 40, 40);
    
    self.navigationItem.titleView =  imageView;
    
    [self createSubView];
    UIButton *musicBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    musicBtn.frame = CGRectMake(0, 0, 40, 40);
    
    [musicBtn setImage:[UIImage imageNamed:@"music.png"] forState:UIControlStateNormal];
    [musicBtn addTarget:self action:@selector(goToMusicViewController:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:musicBtn];
    
}

- (void)goToMusicViewController:(UIButton *)sender {
    WSMusicViewController *music = [[WSMusicViewController alloc] init];
    [self.navigationController pushViewController:music animated:YES];
}
- (void)createSubView {
    
    WSJSportView *sportView = [[WSJSportView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 113)];
    [sportView.StartRunningBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:sportView];
    self.sportView = sportView;
}
- (void)click:(UIButton *)sender {

    WSJSportBeginViewController *sport = [[WSJSportBeginViewController alloc] init];
    [self presentViewController:sport animated:YES completion:nil];

}

-(void)leftButtonAction{
    
    WSJSportDataRecordViewController *dataRecord = [[WSJSportDataRecordViewController alloc] init];
    dataRecord.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:dataRecord animated:YES];
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
